package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.OrderMapper;
import com.molichuxing.services.infrastructure.dto.request.create.OrderCreateDto;
import com.molichuxing.services.infrastructure.dto.response.OrderDto;
import com.molichuxing.services.infrastructure.dto.response.OrderSubcompanyDto;
import com.molichuxing.services.infrastructure.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author renzhengyu
 * @since 2019-08-12
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
    private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Resource
    private OrderMapper orderMapper;

    @Override
    public OrderDto getOrder(Long orderCode) {
        return orderCode == null ? null : Convert.toOrderBeanDto(orderMapper.getByOrderCode(orderCode));
    }

    /**
     * 新增订单表信息
     *
     * @param createDto
     * @return 订单表id
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Integer create(OrderCreateDto createDto) {
        return orderMapper.create(Convert.toOrderBean(createDto));
    }

    /**
     * 查询订单和资产公司
     *
     * @param orderCodes
     * @return
     */
    @Override
    public Map<Long, OrderSubcompanyDto> getOrderAndSubcompany(List<Long> orderCodes) {
        List<OrderSubcompanyDto> list = orderMapper.getOrderAndSubcompany(orderCodes);
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.stream().collect(Collectors.toMap(OrderSubcompanyDto::getOrderCode, dto -> dto));
    }

}
